home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Tetris Light 1.0.3 / source / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  8.0 KB  |  363 lines  |  [TEXT/CWIE]

  1. /* ----------------------------------------------------------------------
  2. File: main.c
  3.  
  4. Purpose:    This is the main module for the Tetris program.
  5.             
  6. Tetris Light - a simple implementation of a Tetris game
  7. Copyright © 1993-1996 Hoylen Sue
  8.  
  9. Updated for CW9 by Paul Celestin
  10. Questions about this version should go to me at celestin@celestin.com
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program; see the file COPYING.  If not, write to the
  24. Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  25. ---------------------------------------------------------------------- */
  26.  
  27. #include "local.h"
  28.  
  29. #include "alert.h"
  30. #include "controls.h"
  31. #include "game.h"
  32. #include "highscore.h"
  33. #include "windows.h"
  34. #include "menu_da.h"
  35. #include "resources.h"
  36. #include "pref_file.h"
  37.  
  38. /*--------------------------------------------------------------------*/
  39.  
  40. /* Globals */
  41.  
  42. static EventRecord    evt;
  43. static Boolean        in_foreground = TRUE;
  44.  
  45. static Boolean        program_finished = FALSE;
  46.  
  47. static INTEGER pref_file;
  48. static Boolean pref_file_valid = FALSE;
  49.  
  50. /*--------------------------------------------------------------------*/
  51.  
  52. /* Local Prototypes */
  53.  
  54. static void handle_mouseDown(void);
  55. static void handle_mf_event(void);
  56.  
  57. static void start_up(void);
  58. static void shut_down(void);
  59.  
  60. /*--------------------------------------------------------------------*/
  61.  
  62. static pascal void sys_error_resume(void)
  63. /* Resume routine for system error. Just exits, but is better than 
  64.    forcing users to reboot. */
  65. {
  66.     ExitToShell();
  67. }
  68.  
  69. static void tool_box_init(void)
  70. /* General initialization calls.  Called at the beginning of the program. */
  71. {
  72.     InitGraf(&qd.thePort);
  73.     InitFonts();
  74.     FlushEvents(everyEvent, 0);    /* Remove all events */
  75.     InitWindows();
  76.     InitMenus();
  77.     TEInit();
  78.     InitDialogs(0L);
  79.     InitCursor();
  80. }
  81.  
  82. /*--------------------------------------------------------------------*/
  83.  
  84. static void process_arg_files(void)
  85. /* Process the file supplied when opening the application. */
  86. {
  87.     INTEGER message = 0;
  88.     INTEGER count = 0;
  89.     
  90. #if !GENERATINGCFM
  91.     CountAppFiles(&message, &count);
  92.     
  93.     if (count > 0) {
  94.         INTEGER index;
  95.         
  96.         if (message != appOpen)
  97.             ExitToShell();
  98.  
  99.         for (index = 1; index <= count; index++) {
  100.             AppFile info;
  101.             
  102.             GetAppFiles(index, &info);
  103.             if (info.fType == SAVE_FILE_SIGNATURE)
  104.                 game_load(info.fName, info.vRefNum);
  105.             ClrAppFiles(index);
  106.         }
  107.     }
  108. #endif
  109. }
  110.  
  111.  
  112. /*--------------------------------------------------------------------*/
  113.  
  114. void main(void)
  115. /* The main function. Includes the main event loop. */
  116. {
  117.     Boolean WNE_implemented = TRUE;
  118.     unsigned long secs;
  119.     
  120.     /* Initialization */
  121.     
  122.     tool_box_init();
  123.     
  124.     if (menu_init())
  125.         alert_fatal(1);
  126.     if (game_init())
  127.         alert_fatal(2);
  128.     if (highscore_init())
  129.         alert_fatal(3);        
  130.     if (controls_init())
  131.         alert_fatal(4);
  132.     
  133.     GetDateTime(&secs);
  134.     game_new();
  135.     process_arg_files();
  136.  
  137.     start_up();
  138.     
  139.     /* Main event loop */
  140.     
  141.     while (! program_finished) {
  142.         if (WNE_implemented)
  143.             WaitNextEvent(everyEvent, &evt, 0L, NIL);
  144.         else {
  145.             SystemTask();
  146.             GetNextEvent(everyEvent, &evt);
  147.         }
  148.         
  149.         switch (evt.what) {
  150.         case mouseDown:
  151.             handle_mouseDown();
  152.             break;
  153.         case mouseUp:
  154.             /* ignored */
  155.             break;
  156.         case autoKey:
  157.         case keyDown:            
  158.             if (evt.modifiers & cmdKey) {
  159.                 adjust_menus();
  160.                 program_finished = menu_choice(MenuKey(evt.message &
  161.                                                        charCodeMask));
  162.             }
  163.             else {
  164.                 register WindowPtr wind = FrontWindow();
  165.                 
  166.                 if (wind && ! is_DA_window(wind))
  167.                     window_key(wind, (evt.message & keyCodeMask) >> 8,
  168.                                evt.message & charCodeMask);
  169.             }
  170.             break;
  171.         case updateEvt:
  172.             window_update((WindowPtr) evt.message);
  173.             break;
  174.         case activateEvt:
  175.             if (evt.modifiers & activeFlag)
  176.                 window_activate((WindowPtr) evt.message);
  177.             else
  178.                 window_deactivate((WindowPtr) evt.message);
  179.             break;
  180.         case osEvt:
  181.             handle_mf_event();
  182.             break;
  183.         }
  184.         
  185.         game_periodic();
  186.     }
  187.     
  188.     shut_down();
  189.     
  190.     game_term();
  191.     highscore_term();
  192.     
  193.     ExitToShell();
  194. }
  195.  
  196. /*--------------------------------------------------------------------*/
  197.  
  198. static void handle_mouseDown(void)
  199. /* Handles mouseDown events from the main event loop. Dispatches them
  200.    to the appropriate handler. */
  201. {
  202.     WindowPtr    wind;
  203.  
  204.     switch (FindWindow(evt.where, &wind)) {
  205.     case inDesk:
  206.         /* ignore */
  207.         break;
  208.     case inMenuBar:
  209.         adjust_menus ();
  210.         program_finished = menu_choice(MenuSelect(evt.where));
  211.         break;
  212.     case inSysWindow:
  213.         SystemClick(&evt, wind);
  214.         break;
  215.     case inContent:
  216.         if (FrontWindow() == wind)
  217.             window_mouseDown(wind, evt.where);
  218.         else
  219.             SelectWindow(wind);
  220.         break;
  221.     case inDrag:
  222.         DragWindow(wind, evt.where, &((*LMGetGrayRgn())->rgnBBox));
  223.     case inGrow:
  224.         break;
  225.     case inGoAway:
  226.         program_finished = TrackGoAway(wind, evt.where);
  227.         break;
  228.     default:
  229.         /* Should not get here */
  230.         SysBeep(5);
  231.         break;
  232.     }
  233. }
  234.  
  235. /*--------------------------------------------------------------------*/
  236.  
  237. static void handle_mf_event(void)
  238. /* Handles the multifinder events. */
  239. {
  240.     register unsigned char type = (evt.message & osEvtMessageMask) >> 24;
  241.     
  242.     switch (type) {
  243.       case suspendResumeMessage:
  244.           if (evt.message & resumeFlag) {
  245.               /* Resume */
  246.               register WindowPtr front = FrontWindow();
  247.               
  248.               if (! is_DA_window(front))
  249.                 window_activate(front);
  250.               in_foreground = TRUE;
  251.           }
  252.           else {
  253.               /* Suspend */
  254.               register WindowPtr front = FrontWindow();
  255.               
  256.               if (! is_DA_window(front))
  257.                 window_deactivate(front);
  258.               in_foreground = FALSE;
  259.           }
  260.           break;
  261.     case mouseMovedMessage:
  262.         /* Ignore */
  263.         break;
  264.     }
  265. }
  266.  
  267. /*--------------------------------------------------------------------*/
  268.  
  269. static Boolean option_key_pressed(void)
  270. /* Determines if the option key is pressed.  Returns true if it is. */
  271. {
  272.     static const k_option = 58;
  273.     //unsigned char map[16];
  274.     KeyMap map;
  275.     
  276.     GetKeys(map);
  277.     
  278.     if ((map[k_option >> 3] >> (k_option & 7)) & 1) 
  279.         return TRUE;
  280.     else 
  281.         return FALSE;
  282. }
  283.  
  284. /*--------------------------------------------------------------------*/
  285.  
  286. static void start_up(void)
  287. /* Opens the preference file, and loads up the information from it. */
  288. {
  289.     register OSErr erc;
  290.     
  291.     erc = pref_open(&pref_file, PREF_FILE_STR_ID,
  292.                     CREATOR_SIGNATURE, PREF_FILE_SIGNATURE, FALSE);
  293.     if (erc == noErr) {
  294.         pref_file_valid = TRUE;
  295.         if (high_score_load())
  296.             alert_caution(1);
  297.         controls_load();
  298.         game_load_location();
  299.         highscore_load_location();
  300.     }
  301.     
  302.     game_begin();
  303. }
  304.  
  305. /*--------------------------------------------------------------------*/
  306.  
  307. static void shut_down(void)
  308. /* Writes out information to the preference file (creating one if it
  309.    did not exist). */
  310. {
  311.     register OSErr erc;
  312.  
  313.     if (! pref_file_valid) {
  314.         erc = pref_open(&pref_file, PREF_FILE_STR_ID,
  315.                         CREATOR_SIGNATURE, PREF_FILE_SIGNATURE, TRUE);
  316.         if (erc == noErr)
  317.             pref_file_valid = TRUE;
  318.         else {
  319.             StringHandle sh = GetString(PREF_FILE_STR_ID);
  320.             
  321.             HLock((Handle)sh);
  322.             ParamText(*sh, 0, 0, 0);
  323.             alert_erc(2, erc);
  324.             HUnlock((Handle)sh);
  325.         }
  326.     }
  327.     
  328.     /* Write out preference information */
  329.     
  330.     if (pref_file_valid) {
  331.         erc = high_score_save(pref_file);
  332.         if (erc != noErr)
  333.             alert_erc(6, erc);
  334.         erc = controls_save(pref_file);
  335.         if (erc != noErr)
  336.             alert_erc(6, erc);
  337.         erc = game_save_location(pref_file);
  338.         if (erc != noErr)
  339.             alert_erc(6, erc);
  340.         erc = highscore_save_location(pref_file);
  341.         if (erc != noErr)
  342.             alert_erc(6, erc);
  343.     }
  344.     
  345.     /* Close the preference file */
  346.     
  347.     if (pref_file_valid) {
  348.         CloseResFile(pref_file);
  349.         erc = ResError();
  350.         if (erc != noErr) {
  351.             StringHandle sh = GetString(PREF_FILE_STR_ID);
  352.             
  353.             HLock((Handle)sh);
  354.             ParamText(*sh, 0, 0, 0);
  355.             alert_erc(5, erc);
  356.             HUnlock((Handle)sh);
  357.         }        
  358.         pref_file_valid = FALSE;
  359.     }
  360. }
  361.  
  362. /*--------------------------------------------------------------------*/
  363.